﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;

namespace Freshome.m.vws
{
    /// <summary>
    /// Healtholds.xaml 的交互逻辑
    /// </summary>
    public partial class Healtholds : UserControl
    {
        private const string _ep = "wsHttp_IMultint";

        public Healtholds()
        {
            InitializeComponent();
            this.topanel.Loaded += new RoutedEventHandler(topanel_Loaded);
        }

        void topanel_Loaded(object sender, RoutedEventArgs e)
        {
            var hds = this.DataContext as vms.Healtholds;
            if (hds != null)
            {
                CollectionViewSource vsr = new CollectionViewSource();
                vsr.Source = hds.P_mjs;
                this.cbr.ItemsSource = vsr.View;
                CollectionViewSource vsw = new CollectionViewSource();
                vsw.Source = hds.P_mjs;
                this.cbw.ItemsSource = vsw.View;
            }
        }
        private void Delete_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            var hds = this.DataContext as vms.Healtholds;
            var dg = sender as DataGrid;
            if (hds != null && dg != null)
            {
                if (dg.Name == "dgh") e.CanExecute = dgh.SelectedItem != null && hds.Enabled;
                else if (dg.Name == "dgr") e.CanExecute = dgr.SelectedItem != null && hds.Enabled;
                else e.CanExecute = dgw.SelectedItem != null && hds.Enabled;
            }
            else e.CanExecute = false;
        }
        private void Delete_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            var dg = sender as DataGrid;
            var hds = this.DataContext as vms.Healtholds;
            if (dg != null && hds != null)
            {
                var r0 = dgh.SelectedItem as mds.Healthold;
                if (dg.Name == "dgh")
                {
                    if (r0 != null)
                    {
                        if (r0.Created == false)
                        {
                            if (MessageBox.Show(string.Format("{0} 的数据将被删除，继续吗？", r0.Name), "保健科目",
                                MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                            {
                                hds.Helproxy.Syncall(cln =>
                                {
                                    if (cln.DeleHealthold(r0.Raw.Mid)) hds.P_holds.Remove(r0);
                                }, new Rmt.MultintClient(_ep));
                            }
                        }
                        else hds.P_holds.Remove(r0);
                    }
                }
                else if (dg.Name == "dgr")
                {
                    var r1 = dgr.SelectedItem as mds.RightMate;
                    if (r0 != null && r1 != null)
                    {
                        if (r1.Created == false)
                        {
                            if (MessageBox.Show(string.Format("{0} 的适宜主料 {1} 将被删除，继续吗？", r0.Name, hds.P_mjs[r1.Jmx]),
                                "适宜主料", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                            {
                                hds.Helproxy.Syncall(cln =>
                                {
                                    if (cln.DeleRawithealth(r1.Raw.Mid)) r0.Rights.Remove(r1);
                                }, new Rmt.MultintClient(_ep));
                            }
                        }
                        else r0.Rights.Remove(r1);
                    }
                }
                else
                {
                    var r1 = dgw.SelectedItem as mds.WrongMate;
                    if (r0 != null && r1 != null)
                    {
                        if (r1.Created == false)
                        {
                            if (MessageBox.Show(string.Format("{0} 的禁忌主料 {1} 将被删除，继续吗？", r0.Name, hds.P_mjs[r1.Jmx]),
                                "禁忌主料", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                            {
                                hds.Helproxy.Syncall(cln =>
                                {
                                    if (cln.DeleRawithealth(r1.Raw.Mid)) r0.Wrongs.Remove(r1);
                                }, new Rmt.MultintClient(_ep));
                            }
                        }
                        else r0.Wrongs.Remove(r1);
                    }
                }
            }
        }
    }
}
